package rps.client.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import rps.game.data.Figure;
import rps.game.data.Player;

public class GameButton extends JButton{

	private static final long serialVersionUID = 6600020995485047811L;
	private Figure figure;
	private Player player;
	private int index;
	private boolean selected = false;
	private boolean attackble = false;
	public static int UP = 0,
			DOWN = 1,
			LEFT=2,
			RIGHT=3;	
	private int possibleMove=-1;
	private int lastMove=-1;
	private Border selectedBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED, new Color(140,90,60), new Color(90,60,40));
	private Border defaultBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, new Color(140,90,60), new Color(90,60,40));
	private Border disabledBorder = BorderFactory.createLineBorder(Color.GRAY);
	
	public GameButton(int index){
		this.index = index;
		this.setContentAreaFilled(false);
		this.setEnabled(false);
	}
	public void setFigure(Figure figure) {
		this.figure = figure;
	}
	public Figure getFigure() {
		return figure;
	}
	public void setPlayer(Player player) {
		this.player = player;
	}
	public Player getPlayer() {
		return player;
	}
	public int getIndex() {
		return index;
	}
	public void setSelected(boolean selected) {
		this.selected = selected;
	}
	public boolean getSelected(){
		return  selected;
	}
	public void setAttackble(boolean attackble) {
		this.attackble = attackble;
	}
	public boolean isAttackble() {
		return attackble;
	}
	public void setPossibleMove(int possibleMove) {
		this.possibleMove = possibleMove;
	}
	public int getPossibleMove() {
		return possibleMove;
	}
	public int getLastMove() {
		return lastMove;
	}
	public void setLastMove(int lastMove) {
		this.lastMove = lastMove;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		if(selected){
			this.setBorder(selectedBorder);
			this.setContentAreaFilled(false);
			this.setBackground(null);
		}
		else if(!isEnabled()){
			this.setBorder(disabledBorder);
			this.setContentAreaFilled(false);
			this.setBackground(null);
		}
		else{
			this.setBorder(defaultBorder);
			this.setContentAreaFilled(false);
			this.setBackground(null);
			if(attackble){
				this.setContentAreaFilled(true);
				this.setBackground(Color.RED);
			}
		}
		
		Image img = MediaProvider.getMediaProvider().buttonImage(figure, player, lastMove, possibleMove);
		if(figure!=null){
			g.drawImage(img, 10, 10, 80, 80, this);
		}
		else if(img!=null){
			g.drawImage(img, (100-img.getWidth(this))/2, (100-img.getHeight(this))/2, this);
		}
	}
}

